<?php
/**
 * 商品控制器
 */
class ProductController extends CommonController{
	//保存实例化模型对象
	private $_model;
	//构造函数
	public function __init(){
		//实例化tagmodel.class.php
		$this->_model=K('Product');
	}
	//商品首页列表
	public function index(){

		//查询所有链接,如果需要分页传参true
		$data=$this->_model->get_all_data(Null,true);
		// P($data);die;
		//分配分页变量
		$this->assign('page',$data['page']);
		//在分页分配完成之后，$data['page'] 就没有用了，
		//所以删除掉它，要不循环会有问题
		unset($data['page']);

		$this->assign('data',$data);
		$this->display();
	}

	/**
     * 上传列表图
     */
    public function upload(){
        $upload = new Upload('Upload/product/list' . date('ymd') .'/');
        $file = $upload->upload();
        if (empty($file)) {
            $this->ajax('上传失败');
        } else {
        	$img= new Image();
            $data = $file[0]; 
            $info=$img->thumb($data['path'],'',200,200);
            $data['list_pic']=$info;
            $this->ajax($data);
        }
    }
    	/**
         * 上传图册，大中小
         */
        public function upload2(){
            $upload = new Upload('Upload/product' . date('ymd') .'/');
            $file = $upload->upload();
            if (empty($file)) {
                $this->ajax('上传失败');
            } else {
            	$img= new Image();
                $data = $file[0];
                //文件目录路径
                $dirname=dirname($data['path']);
  
                //$data数组内容如下：
                //{"path":"Upload\/product150112\/3631421046158.jpg",
                //"uptime":1421046158,"fieldname":"Filedata",
                //"basename":"3631421046158.jpg",
                //"filename":"3631421046158",
                //"name":"1","size":75254,"ext":"jpg","
                //dir":"Upload\/product150112",
                //"url":"http:\/\/localhost\/program\/Upload\/product150112\/3631421046158.jpg",
                //"image":1,} 
                //小图 ,参数：原图路径,缩略输出路径，宽高
                $small=$img->thumb($data['path'],$dirname.'/small_'.$data['basename'],60,60);
                //中图
                $mid=$img->thumb($data['path'],$dirname.'/mid_'.$data['basename'],360,360);
                //大图
                $big=$img->thumb($data['path'],$dirname.'/big_'.$data['basename'],720,720);
                //把缩略之后的路径压入数组data
                $data['pr_small_pic']=$small;
                $data['pr_mid_pic']=$mid;
                $data['pr_big_pic']=$big;
                $this->ajax($data);
            }
        }

	//增加商品
	public function add(){
	
		if (IS_POST) {

			//执行模型添加，在添加的动作之前，有自动验证add_data()
			if(!$this->_model->add_data()) $this->error($this->_model->error);

			$this->success('添加成功');
		}
		//查询出所有分类选项
		$data=K('Category')->get_all_data();
		$this->assign('data',$data);
		//查询出所有品牌选项
		$rand=K('Rand')->get_all_data();
		$this->assign('rand',$rand);

		$this->display();
	}

	//修改商品信息
	public function edit(){
		//获得修改链接的id
		$rid=Q('get.rid',0,'interval');
		//更新数据
		if (IS_POST) {
			$this->_model->edit_data(array('rid'=>$rid));
			$this->success('修改成功！',U('index'));
		}
		
		//获得旧数据，取一条//获得当前元素
		$oldData=current($this->_model->get_all_data(array('rid'=>$rid)));
		// p($oldData);die;
		//分配旧数据
		$this->assign('oldData',$oldData);
		
		//查询出所有分类选项
		$data=K('Category')->get_all_data();
		$this->assign('data',$data);


		$this->display();
	}

	//删除商品
	public function del(){
		//获得删除的链接的id
		$rid=Q('get.rid',0,'interval');
		// p($fid);die;
		//执行删除
		if ($this->_model->del_data(array('rid'=>$rid))) $this->success('删除成功！',U('index'));

	}


}
